const express = require('express')
const service = require('./authentication.service')
const validateRequest = require('../../../middleware/validateRequest')
const validation = require('./authentication.validation')
const knexClient = require('../../../helper/knexClient')

const router = express.Router()

// get access token
router.post(
  '/authentication',
  validateRequest(validation.createAccessToken),
  async (req, res, next) => {
    try {
      const params = req.body
      const token = await service.createAccessToken(knexClient, params)
      return res.json({ token })
    } catch (error) {
      return next(error)
    }
  },
)

module.exports = router
